package com.smartlbs.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.druid.stat.TableStat.Name;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.smartlbs.baseframe.utils.ConfigFileUtils;
import com.smartlbs.baseframe.utils.DateUtils;
import com.smartlbs.baseframe.utils.HttpClientUtil;
import com.smartlbs.baseframe.utils.ParseExcelUtil;
import com.smartlbs.baseframe.web.ResponseInfo;
import com.smartlbs.dto.BetInfoDto;
import com.smartlbs.entity.BetInfo;
import com.smartlbs.entity.OtherBet;
import com.smartlbs.entity.OtherBet.GE;
import com.smartlbs.entity.OtherBet.betPlayer;
import com.smartlbs.entity.OtherMatch.player;
import com.smartlbs.enums.BetType;
import com.smartlbs.mapper.BetInfoMapper;
import com.smartlbs.service.IBetInfoService;
import com.smartlbs.service.ISettingService;
import com.smartlbs.util.StringUtil;
import com.smartlbs.vo.BetInfoVo;

@Service
public class BetInfoServiceImpl implements IBetInfoService {
	@Autowired
	private BetInfoMapper mapper;
	@Autowired
	private ISettingService setService;

	@Override
	public int insert(BetInfo info) throws Exception {
		// TODO Auto-generated method stub
		return mapper.insert(info);
	}

	@Override
	public int update(BetInfo info) throws Exception {
		// TODO Auto-generated method stub
		return mapper.update(info);
	}

	@Override
	public int delete(long infoId) throws Exception {
		// TODO Auto-generated method stub
		return mapper.delete(infoId);
	}

	@Override
	public BetInfoVo findByid(long infoId) throws Exception {
		// TODO Auto-generated method stub
		BetInfo findById = mapper.findById(infoId);
		BetInfoVo betInfoVo = new BetInfoVo();
		BeanUtils.copyProperties(findById, betInfoVo);
		return betInfoVo;
	}

	@Override
	public List<BetInfo> find(BetInfoDto infoDto) {
		// TODO Auto-generated method stub
		return mapper.find(infoDto);
	}

	@Override
	public void insertBet(long ci, long gameId,String name) throws Exception {
		// TODO Auto-generated method stub

		OtherBet betlist = getBetlist(ci,name);
		if (betlist != null) {
			List<GE> ges = betlist.getGE();

			for (GE ge : ges) {
				BetInfo betInfo = new BetInfo();
				betInfo.setGameId(gameId);
				betInfo.setSource(1);
				betInfo.setOutId(String.valueOf(ci));
				Object json1 = JSONObject.toJSONString(ge,
						SerializerFeature.WriteMapNullValue);
				betInfo.setBetInfo(json1.toString());
				betInfo.setStatus(1);
				betInfo.setCreateTime(new Date());
				int inset = insert(betInfo);
			}
		}
	}

	public OtherBet getBetlist(long ci,String name) throws Exception {
		String beturl = "https://1xbet788.com/LineFeed/GetGameZip?id="
				+ ci
				+ "&lng=cn&cfview=0&isSubGames=true&GroupEvents=true&allEventsGroupSubGames=true&countevents=250&partner=57";
		String betjson = HttpClientUtil.doGets(beturl,name);
		String jsonstr = "";
		
		OtherBet parseObject=null;
		if(!StringUtils.isEmpty(betjson)){
			 parseObject = JSON.parseObject(JSON.parseObject(betjson)
						.getString("Value"), OtherBet.class);
		}
		
		 if(parseObject!=null){
			// 选手词典
				Map<String, String> playerNames = ParseExcelUtil.getPlayerNames();
				List<GE> ge = parseObject.getGE();
				for (GE ge2 : ge) {
					List<List<betPlayer>> e = ge2.getE();
					for (List<betPlayer> list : e) {
						for (betPlayer betPlayer : list) {
							player pl = betPlayer.getPL();
							if (pl != null) {
								if (!StringUtils.isEmpty(pl.getN())) {
									String plname = playerNames.get(pl.getN().toLowerCase());
									if (!StringUtils.isEmpty(plname)) {
										pl.setN(plname);
									}
								}
							}
						}
					}
				}
		 }
		
		return parseObject;
	}

	@Override
	public List<BetInfoVo> findbyGameId(Long gameId) throws Exception {
		// TODO Auto-generated method stub
		Map<String, Object> valueMap=ConfigFileUtils.getBetJson();
		
		List<BetInfoVo> list = new ArrayList<>();
		List<BetInfo> betInfos = mapper.findbyGameId(gameId);
		for (BetInfo betInfo : betInfos) {
			BetInfoVo vo = new BetInfoVo();
			BeanUtils.copyProperties(betInfo, vo);
			if (!StringUtils.isEmpty(vo.getBetInfo())) {
				GE g = JSON.parseObject(vo.getBetInfo(), GE.class);
				List<List<betPlayer>> e = g.getE();
				Object object = valueMap.get(String.valueOf(g.getG()));
				g.setgName(object);
				
				vo.setGe(g);
			}
			list.add(vo);

		}

		return list;
	}

	@Override
	public void insert(long id, List<GE> ges) throws Exception {
		// TODO Auto-generated method stub
		for (GE ge : ges) {

			BetInfo betInfo = new BetInfo();
			betInfo.setGameId(id);
			betInfo.setSource(1);
			Random random = new Random();
			int nextInt = random.nextInt(10000000);
			betInfo.setOutId(String.valueOf(100000000 + nextInt));
			Object json1 = JSONObject.toJSONString(ge,
					SerializerFeature.WriteMapNullValue);
			betInfo.setBetInfo(json1.toString());
			betInfo.setCreateTime(new Date());
			betInfo.setSource(2);
			betInfo.setStatus(1);
			int inset = insert(betInfo);
		}

	}
}
